package eabnp.basic.util;

import eabnp.basic.result.GResult;
import java.lang.reflect.Method;

public final class ClassUtil
{
  public static final String JAVA_PACKAGE_SEPARATOR = ".";

  public static String toString(Object obj)
  {
    if (obj == null) return null;
    return obj.toString();
  }

  public static boolean isEquals(Object obj1, Object obj2)
  {
    if ((obj1 == null) && (obj2 == null)) return true;

    if ((obj1 == null) || (obj2 == null)) return false;

    if (!obj1.getClass().getName().equals(obj2.getClass().getName())) {
      return false;
    }
    return obj1.equals(obj2);
  }

  public static GResult newInstanceOfClass(String className)
  {
    if (StringUtil.isNullOrEmptyStr(className)) {
      return GResult.newFailureResult("类名为空").addErrMsgPreStr("新建类实例失败")
        .toGResult();
    }
    Object obj;
    try
    {
      Class c = Class.forName(className);
      obj = c.newInstance();
    }
    catch (Exception ex)
    {
      return GResult.newFailureResult("新建类实例失败", ex);
    }
    return GResult.newSuccessResult(obj);
  }

  public static GResult getInstanceOfClass(String className)
  {
    return getInstanceOfClass(className, "getInstance", 
      null, null);
  }

  public static GResult getInstanceOfClass(String className, String instanceGetterMethodName, Class[] parameterTypes, Object[] args)
  {
    if (StringUtil.isNullOrEmptyStr(className)) {
      return GResult.newFailureResult("类名为空");
    }

    if (StringUtil.isNullOrEmptyStr(instanceGetterMethodName)) {
      return GResult.newFailureResult("类实例获取方法为空");
    }

    Class c = null;
    try {
      c = Class.forName(className);
    } catch (Exception ex) {
      return GResult.newFailureResult(null, "无法找到类 " + className, ex);
    }

    Method m = null;
    try {
      m = c.getMethod(instanceGetterMethodName, parameterTypes);
    } catch (Exception ex) {
      String[] sa = { "无法获取", className, "的实例化方法", instanceGetterMethodName };
      String str = StringUtil.joinStringArray(sa);
      return GResult.newFailureResult(null, str, ex);
    }

    Object obj = null;
    try {
      obj = m.invoke(null, args);
    } catch (Exception ex) {
      return GResult.newFailureResult(null, "调用类" + className + "的实例获取方法失败" + 
        instanceGetterMethodName, ex);
    }

    if (obj == null) {
      return GResult.newFailureResult("调用类" + className + "的实例获取方法返回空值" + 
        instanceGetterMethodName);
    }

    return GResult.newSuccessResult(obj);
  }

  public static boolean isSameOrSuperClass(Class c, Class sc)
  {
    if ((c == null) || (sc == null)) return false;
    if (c.equals(sc)) return true;
    Class cc = c.getSuperclass();
    while (cc != null) {
      if (cc.equals(sc)) return true;
      cc = cc.getSuperclass();
    }

    return false;
  }

  public static boolean isInterfaceImplents(Object obj, Class interfaceClass)
  {
    return isInterfaceImplents(obj.getClass(), 
      interfaceClass.getName());
  }

  public static boolean isInterfaceImplents(Class c, String interfaceName)
  {
    if (c == null) {
      return false;
    }

    if (StringUtil.isNullOrEmptyStr(interfaceName)) {
      return true;
    }

    Class[] is = c.getInterfaces();
    for (int i = 0; i < is.length; i++) {
      if (is[i].getName().equals(interfaceName)) {
        return true;
      }
    }

    Class a = c.getSuperclass();
    while (a != null) {
      is = a.getInterfaces();
      for (int i = 0; i < is.length; i++) {
        if (is[i].getName().equals(interfaceName)) {
          return true;
        }
      }

      a = a.getSuperclass();
    }

    return false;
  }
}